package com.himiotech.core.project.web.system.monitor;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.himiotech.core.framework.helper.anno.Log;
import com.himiotech.core.framework.helper.enums.BusinessType;
import com.himiotech.core.framework.helper.utils.ExcelUtil;
import com.himiotech.core.project.model.entity.job.JobLog;
import com.himiotech.core.project.model.metadata.JobLogMetadata;
import com.himiotech.core.project.service.IJobLogService;
import com.himiotech.core.project.service.IJobService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 定时任务调度日志表 web层
 *
 * @author wuwenbin
 */
@Controller
@RequestMapping("/monitor/jobLog")
public class JobLogController extends HimioBaseController {

    @Autowired
    private IJobLogService jobLogService;
    @Autowired
    private IJobService jobService;

    @RequiresPermissions("monitor:jobLog:view")
    @GetMapping
    public String jobLog(ModelMap mmp, Long jobId) {
        mmp.addAttribute("job", jobService.getById(jobId));
        return "monitor/jobLog";
    }

    /**
     * 查询定时任务调度日志表列表
     *
     * @param page   分页信息
     * @param jobLog 查询条件
     * @return 表格分页数据
     */
    @RequiresPermissions("monitor:jobLog:list")
    @PostMapping("/list")
    @ResponseBody
    public HimioResult list(Page<JobLog> page, JobLog jobLog) {
        handleOrderBy(request, page);
        page = jobLogService.selectJobLogListPage(page, jobLog);
        return writeJsonLayuiTable(page);
    }

    /**
     * 导出定时任务调度日志表列表数据
     *
     * @param jobLog 查询条件
     * @return 导出数据
     */
    @Log(title = "定时任务调度日志表 - 导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("monitor:jobLog:export")
    @PostMapping("/export")
    @ResponseBody
    public HimioResult export(JobLog jobLog) {
        QueryWrapper<JobLog> query = Wrappers.query();
        if (jobLog != null) {
            query.like(ObjectUtil.isNotEmpty(jobLog.getJobName()), JobLogMetadata.JOB_NAME.getColName(), jobLog.getJobName());
            query.eq(ObjectUtil.isNotEmpty(jobLog.getJobGroup()), JobLogMetadata.JOB_GROUP.getColName(), jobLog.getJobGroup());

        }
        List<JobLog> list = jobLogService.list(query);
        ExcelUtil<JobLog> util = new ExcelUtil<>(JobLog.class);
        return util.exportExcel(list, "定时任务调度日志表");
    }


    /**
     * 删除定时任务调度日志表
     *
     * @param ids 删除id（以逗号分隔）
     * @return 处理结果
     */
    @RequiresPermissions("monitor:jobLog:remove")
    @Log(title = "定时任务调度日志表 - 删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public HimioResult remove(String ids) {
        if (ids != null) {
            List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
            return writeJson(jobLogService.removeByIds(idList));
        } else {
            return writeJsonOk("没有需要删除的数据");
        }
    }


}

